<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux进程管理深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9fafb;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #4a6cf7 100%);
        }
        .code-block {
            background-color: #2d3748;
            border-radius: 0.5rem;
            position: relative;
        }
        .code-block pre {
            margin: 0;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 12px;
            left: 12px;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #fc625d;
            box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .signal-card {
            border-left: 4px solid;
        }
        .signal-card.SIGINT {
            border-left-color: #f6ad55;
        }
        .signal-card.SIGTERM {
            border-left-color: #f56565;
        }
        .signal-card.SIGKILL {
            border-left-color: #9f7aea;
        }
        .signal-card.SIGUSR1 {
            border-left-color: #4299e1;
        }
        .ipc-table th {
            background-color: #4a6cf7;
            color: white;
        }
        .ipc-table tr:nth-child(even) {
            background-color: #f7fafc;
        }
        .star-rating {
            color: #f6e05e;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 0.8;
            margin: 0.2rem 0.5rem 0 0;
            color: #4a6cf7;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Linux进程管理深度解析</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-2xl">从基础概念到高级技巧，全面掌握Linux进程管理的艺术</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-microchip mr-2"></i>进程调度</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-project-diagram mr-2"></i>进程通信</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-tachometer-alt mr-2"></i>性能优化</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-4xl py-12 px-4 md:px-0">
        <!-- Section 1 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-blue-100 text-blue-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-cogs text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">一、进程：程序运行的基本单位</h2>
            </div>
            
            <p class="drop-cap mb-6">每个在Linux系统中运行的程序都是一个进程。想象你同时打开浏览器、音乐播放器和代码编辑器，每个程序都对应一个或多个进程。我们可以通过<code class="bg-gray-100 px-2 py-1 rounded">ps aux</code>命令查看当前所有进程：</p>
            
            <div class="code-block mb-8">
                <pre class="text-gray-200"><code>$ ps aux | grep firefox
user     1234  2.5  4.7 1234567 89012 ?      Sl   14:30   2:45 /usr/lib/firefox/firefox</code></pre>
            </div>
            
            <p class="mb-6">每个进程都有：</p>
            <ul class="list-disc pl-8 mb-8 space-y-2">
                <li>唯一PID（进程ID）</li>
                <li>内存空间</li>
                <li>打开的文件描述符</li>
                <li>执行状态（运行/睡眠/停止等）</li>
            </ul>
            
            <h3 class="text-2xl font-semibold mb-4 text-blue-800">实战案例1：用C语言创建子进程</h3>
            <div class="code-block mb-6">
                <pre class="text-gray-200"><code>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        printf("子进程PID: %d\n", getpid());
    } else {
        printf("父进程PID: %d，创建了子进程%d\n", getpid(), pid);
    }
    return 0;
}</code></pre>
            </div>
            <p class="text-gray-600">编译执行后，你会看到父子进程的输出，这是理解进程创建的绝佳起点。</p>
            
            <div class="mt-8 bg-blue-50 border-l-4 border-blue-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-blue-500 mr-2">
                        <i class="fas fa-lightbulb text-xl"></i>
                    </div>
                    <div>
                        <p class="text-blue-800 font-medium">核心概念：</p>
                        <p class="text-blue-700">fork()系统调用会创建一个与父进程几乎完全相同的子进程，两者的主要区别在于fork()的返回值。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 2 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-purple-100 text-purple-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-stopwatch text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">二、进程调度：CPU时间分配的艺术</h2>
            </div>
            
            <p class="mb-6">Linux采用CFS（完全公平调度器），其核心是红黑树数据结构。假设有三个进程A、B、C，它们的权重分别为1、2、3，CFS会按6:3:2的比例分配时间片。</p>
            
            <h3 class="text-2xl font-semibold mb-4 text-purple-800">常用调度命令：</h3>
            <div class="code-block mb-8">
                <pre class="text-gray-200"><code>$ nice -n 5 ./cpu_intensive_task  # 设置优先级
$ chrt -f 99 ./real_time_task    # 设置实时调度策略</code></pre>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4 text-purple-800">实战案例2：优化CPU密集型任务</h3>
            <p class="mb-4">假设我们有一个视频转码服务：</p>
            <ol class="list-decimal pl-8 mb-6 space-y-2">
                <li>使用<code class="bg-gray-100 px-2 py-1 rounded">taskset</code>绑定到特定CPU核</li>
                <li>通过<code class="bg-gray-100 px-2 py-1 rounded">chrt</code>设置为SCHED_FIFO实时策略</li>
                <li>用<code class="bg-gray-100 px-2 py-1 rounded">perf stat</code>分析性能瓶颈</li>
            </ol>
            
            <div class="code-block mb-8">
                <pre class="text-gray-200"><code>$ taskset -c 2,3 chrt -f 99 ffmpeg -i input.mp4 output.avi</code></pre>
            </div>
            
            <div class="mermaid mb-8">
                graph TD
                    A[进程A 权重1] -->|15%| CPU
                    B[进程B 权重2] -->|30%| CPU
                    C[进程C 权重3] -->|55%| CPU
            </div>
            
            <div class="mt-8 bg-purple-50 border-l-4 border-purple-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-purple-500 mr-2">
                        <i class="fas fa-chart-line text-xl"></i>
                    </div>
                    <div>
                        <p class="text-purple-800 font-medium">性能洞察：</p>
                        <p class="text-purple-700">CFS调度器通过虚拟运行时间(vruntime)来确保公平性，每个进程的vruntime增长速率与其权重成反比。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 3 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-red-100 text-red-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-bell text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">三、进程信号：操作系统的紧急通知</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="signal-card SIGINT p-6 bg-white rounded-lg shadow">
                    <h4 class="text-xl font-semibold mb-2">SIGINT (2)</h4>
                    <p>Ctrl+C 中断</p>
                </div>
                <div class="signal-card SIGTERM p-6 bg-white rounded-lg shadow">
                    <h4 class="text-xl font-semibold mb-2">SIGTERM (15)</h4>
                    <p>默认终止信号</p>
                </div>
                <div class="signal-card SIGKILL p-6 bg-white rounded-lg shadow">
                    <h4 class="text-xl font-semibold mb-2">SIGKILL (9)</h4>
                    <p>强制终止</p>
                </div>
                <div class="signal-card SIGUSR1 p-6 bg-white rounded-lg shadow">
                    <h4 class="text-xl font-semibold mb-2">SIGUSR1 (10)</h4>
                    <p>用户自定义</p>
                </div>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4 text-red-800">信号处理三原则：</h3>
            <ol class="list-decimal pl-8 mb-8 space-y-2">
                <li>保持处理函数简单</li>
                <li>避免不可重入函数</li>
                <li>注意信号屏蔽</li>
            </ol>
            
            <h3 class="text-2xl font-semibold mb-4 text-red-800">实战案例3：实现优雅关闭的Web服务器</h3>
            <div class="code-block mb-6">
                <pre class="text-gray-200"><code>void sigterm_handler(int sig) {
    printf("收到终止信号，开始清理...\n");
    close(listen_socket);
    save_session_data();
    exit(0);
}

int main() {
    signal(SIGTERM, sigterm_handler);
    // 服务器主循环
}</code></pre>
            </div>
            
            <div class="mt-8 bg-red-50 border-l-4 border-red-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-red-500 mr-2">
                        <i class="fas fa-exclamation-triangle text-xl"></i>
                    </div>
                    <div>
                        <p class="text-red-800 font-medium">安全提示：</p>
                        <p class="text-red-700">SIGKILL(9)不能被捕获或忽略，总是会立即终止进程。应该优先使用SIGTERM(15)进行优雅关闭。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 4 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-green-100 text-green-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-comments text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">四、进程间通信（IPC）：协作的桥梁</h2>
            </div>
            
            <div class="overflow-x-auto mb-8">
                <table class="ipc-table w-full rounded-lg overflow-hidden">
                    <thead>
                        <tr>
                            <th class="py-3 px-4 text-left">通信方式</th>
                            <th class="py-3 px-4 text-left">适用场景</th>
                            <th class="py-3 px-4 text-left">性能对比</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="py-3 px-4">无名管道</td>
                            <td class="py-3 px-4">父子进程简单通信</td>
                            <td class="py-3 px-4"><span class="star-rating">★★☆</span></td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">命名管道</td>
                            <td class="py-3 px-4">任意进程通信</td>
                            <td class="py-3 px-4"><span class="star-rating">★★☆</span></td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">共享内存</td>
                            <td class="py-3 px-4">大数据量高速通信</td>
                            <td class="py-3 px-4"><span class="star-rating">★★★</span></td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">消息队列</td>
                            <td class="py-3 px-4">结构化数据通信</td>
                            <td class="py-3 px-4"><span class="star-rating">★★☆</span></td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">信号量</td>
                            <td class="py-3 px-4">进程同步</td>
                            <td class="py-3 px-4"><span class="star-rating">★☆☆</span></td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4">Socket</td>
                            <td class="py-3 px-4">跨机器通信</td>
                            <td class="py-3 px-4"><span class="star-rating">★★☆</span></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4 text-green-800">实战案例4：使用共享内存实现实时日志系统</h3>
            <div class="code-block mb-6">
                <pre class="text-gray-200"><code>// 创建共享内存
int shm_id = shmget(IPC_PRIVATE, 1024, IPC_CREAT | 0666);
char *shm_ptr = shmat(shm_id, NULL, 0);

// 写入日志
sprintf(shm_ptr, "[%s] New connection established", get_current_time());

// 读取日志
printf("Latest log: %s\n", shm_ptr);</code></pre>
            </div>
            
            <div class="mermaid mb-8">
                flowchart LR
                    A[日志写入进程] -->|共享内存| B[日志读取进程]
                    A -->|信号| C[监控进程]
                    C -->|消息队列| A
            </div>
            
            <div class="mt-8 bg-green-50 border-l-4 border-green-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-green-500 mr-2">
                        <i class="fas fa-bolt text-xl"></i>
                    </div>
                    <div>
                        <p class="text-green-800 font-medium">性能优化：</p>
                        <p class="text-green-700">共享内存是最快的IPC方式，因为它避免了内核与用户空间之间的数据拷贝，但需要手动处理同步问题。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 5 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-sitemap text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">五、综合实战：构建高并发监控系统</h2>
            </div>
            
            <p class="mb-6">项目需求：</p>
            <ol class="list-decimal pl-8 mb-8 space-y-2">
                <li>实时监控服务器状态</li>
                <li>支持动态调整监控频率</li>
                <li>异常时自动告警</li>
            </ol>
            
            <h3 class="text-2xl font-semibold mb-4 text-indigo-800">架构设计：</h3>
            <div class="code-block mb-8">
                <pre class="text-gray-200">[采集进程] --信号--> [控制进程] --共享内存--> [Web展示进程]
   ↑                      |
   └--------消息队列------┘</pre>
            </div>
            
            <h3 class="text-2xl font-semibold mb-4 text-indigo-800">关键实现步骤：</h3>
            <ol class="list-decimal pl-8 mb-8 space-y-2">
                <li>使用<code class="bg-gray-100 px-2 py-1 rounded">inotify</code>监控系统文件变化</li>
                <li>通过信号量同步数据访问</li>
                <li>采用epoll实现高效I/O</li>
                <li>使用UNIX域Socket进行控制通信</li>
            </ol>
            
            <h3 class="text-2xl font-semibold mb-4 text-indigo-800">性能优化点：</h3>
            <ul class="list-disc pl-8 mb-8 space-y-2">
                <li>为采集进程设置实时优先级</li>
                <li>使用内存映射文件处理大日志</li>
                <li>采用双缓冲技术避免数据竞争</li>
            </ul>
            
            <div class="mermaid mb-8">
                graph TB
                    subgraph 监控系统架构
                        A[采集进程] -->|信号| B[控制进程]
                        B -->|共享内存| C[Web展示进程]
                        A -->|消息队列| B
                    end
            </div>
            
            <div class="mt-8 bg-indigo-50 border-l-4 border-indigo-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-indigo-500 mr-2">
                        <i class="fas fa-cogs text-xl"></i>
                    </div>
                    <div>
                        <p class="text-indigo-800 font-medium">架构决策：</p>
                        <p class="text-indigo-700">这种混合IPC架构充分利用了各种通信方式的优势：信号用于紧急通知，共享内存用于大数据传输，消息队列用于结构化命令。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section 6 -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-yellow-100 text-yellow-800 rounded-full w-12 h-12 flex items-center justify-center mr-4">
                    <i class="fas fa-rocket text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">六、进程管理进阶技巧</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card bg-white p-6 rounded-lg shadow">
                    <h3 class="text-xl font-semibold mb-4 text-yellow-800">1. cgroups资源控制</h3>
                    <div class="code-block mb-4">
                        <pre class="text-gray-200"><code># 限制内存使用
$ cgcreate -g memory:/myapp
$ echo 100M > /sys/fs/cgroup/memory/myapp/memory.limit_in_bytes
$ cgexec -g memory:myapp ./memory_hungry_app</code></pre>
                    </div>
                    <p>cgroups可以限制进程组的CPU、内存、IO等资源使用。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg shadow">
                    <h3 class="text-xl font-semibold mb-4 text-yellow-800">2. namespace隔离技术</h3>
                    <div class="code-block mb-4">
                        <pre class="text-gray-200"><code>unshare(CLONE_NEWPID | CLONE_NEWNS);  // 创建新PID和mount命名空间</code></pre>
                    </div>
                    <p>命名空间提供进程隔离，是容器技术的基础。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg shadow">
                    <h3 class="text-xl font-semibold mb-4 text-yellow-800">3. 核心转储分析</h3>
                    <div class="code-block mb-4">
                        <pre class="text-gray-200"><code>$ ulimit -c unlimited
$ gdb ./crash_app core.dump
(gdb) bt  # 查看调用栈</code></pre>
                    </div>
                    <p>通过核心转储可以分析程序崩溃时的状态。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg shadow">
                    <h3 class="text-xl font-semibold mb-4 text-yellow-800">4. 动态追踪工具</h3>
                    <ul class="list-disc pl-6 space-y-1">
                        <li>perf：性能分析</li>
                        <li>strace：系统调用跟踪</li>
                        <li>bpftrace：高级内核追踪</li>
                    </ul>
                </div>
            </div>
            
            <div class="mt-8 bg-yellow-50 border-l-4 border-yellow-500 p-4">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-yellow-500 mr-2">
                        <i class="fas fa-graduation-cap text-xl"></i>
                    </div>
                    <div>
                        <p class="text-yellow-800 font-medium">深入学习：</p>
                        <p class="text-yellow-700">现代Linux系统提供了丰富的进程观察和调试工具，掌握这些工具可以显著提高系统排错和性能调优能力。</p>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-4xl px-4 text-center">
            <div class="mb-6">
                <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                <p>深入技术细节，探索系统原理</p>
            </div>
            <div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors" target="_blank">
                    <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>